function getPostData(req) {
  const promise = new Promise((resove, reject) => {
    // 如果是 get
    if (req.method !== 'POST') {
      resove({});
      return;
    }

    // 如果
    if (req.headers['content-type'] !== 'application/json') {
      resove({});
      return;
    }

    let postData = '';
    req.on('data', chunk => {
      postData += chunk.toString();
    });
    req.on('end', () => {
      if (!postData) {
        resove({});
        return;
      }
      resove(JSON.parse(postData));
    });
  });

  return promise;
}

function handlerCookie(cookieString) {
  const cookie = {};
  cookieString.split(';').forEach(item => {
    if (!item) return;
    const arr = item.split('=');
    const key = arr[0].trim();
    const val = arr[1].trim();
    cookie[key] = val;
  });
  return cookie;
}

function getCookieExpires() {
  const d = new Date();
  d.setTime(d.getTime() + (24 * 60 * 60 * 1000));
  return d.toGMTString();
}

module.exports = {
  getPostData,
  handlerCookie,
  getCookieExpires
}